<template>
	<view class="userInfo">
		<view class="userInfoBox">

			<view class="infoitem">
				<view class="">
					头像
				</view>
				<view class="right">
					<view class="" @click="handleAvatar">
						<image v-if="avatar == null" src="../../static/image/avatar.jpg" mode=""
							style="width: 88rpx;height: 88rpx; border-radius: 88rpx;"></image>
						<image v-else :src="avatar" mode="" style="width: 88rpx;height: 88rpx; border-radius: 88rpx;">
						</image>
					</view>
					<image class="" src="@/static/image/right.png"
						style="width: 16rpx;height: 28rpx;margin-left: 10rpx;" />
				</view>
			</view>

			<view class="infoitem">
				<view class="">
					昵称
				</view>
				<view class="right">
					<view class="">
						<input type="text" style="text-align: right;" v-model="nickname" placeholder="请输入昵称">
					</view>
					<image class="" src="@/static/image/bj.png"
						style="width: 30rpx;height: 30rpx;margin-left: 10rpx;" />
				</view>
			</view>
			<view class="infoitem">
				<view class="">
					性别
				</view>
				<view class="right">
					<view class="" @click="sexshow = true">
						<view v-if="sex=='' || sex=='null'" class="" style="color: #808080;font-size: 32rpx;">
							请选择性别
						</view>
						<view v-else class="">
							{{sex}}
						</view>
						<!-- <input type="text" style="text-align: right;" v-model="sex" placeholder="请选择性别"> -->
					</view>
					<image class="" src="@/static/image/right.png"
						style="width:16rpx;height: 28rpx;margin-left: 10rpx;" />
				</view>
			</view>

			<view class="infoitem">
				<view class="">
					生日
				</view>
				<view class="right" @click="show = true">
					<view class="">
						<view v-if="date1 =='' || date1 =='null'" class="" style="color: #808080;font-size: 32rpx;">
							请选择出生日期
						</view>
						<view v-else class="">
							{{date1}}
						</view>
					</view>
					<image class="" src="@/static/image/right.png"
						style="width: 16rpx;height: 28rpx;margin-left: 10rpx;" />
				</view>
			</view>

			<!-- 	<view class="infoitem">
				<view class="">
					绑定手机号
				</view>
				<view class="right">
					<view class="">
						<input type="text" style="text-align: right;" v-model="phone" placeholder="绑定手机号">
					</view>
					<image class="" src="@/static/image/right.png"
						style="width: 16rpx;height: 28rpx;margin-left: 10rpx;" />
				</view>
			</view> -->


		</view>

		<view class="btns" @click="handleSave">
			确认修改
		</view>

		<u-picker :show="sexshow" :columns="columns" @cancel="sexshow = false" @confirm="handleconfirmSex"
			:closeOnClickOverlay="true" @close="sexshow = false"></u-picker>

		<u-datetime-picker :show="show" v-model="date" mode="date" @cancel="show = false" @confirm="handleconfirmDate"
			:closeOnClickOverlay="true" @close="show = false" :minDate="108752480000"></u-datetime-picker>


	</view>

</template>

<script>
	import {
		userinfo,
		checkuser
	} from "@/api/api.js"
	export default {
		data() {
			return {
				sexshow: false,
				show: false,
				nikeName: "",
				pageData: {},
				avatar: null,
				nickname: "",
				date: "",
				date1: "",
				sex: "",
				columns: [
					['男', '女']
				],
				phone: null,
			}
		},
		onLoad() {
			if (!uni.getStorageSync("token")) {
				uni.reLaunch({
					url: "../login/index"
				})
				return
			}
			this.handleGetuserinfo()
		},
		methods: {
			async handleGetuserinfo() {
				let res = await userinfo()
				this.pageData = res.data
				this.avatar = this.pageData.avatar == null || this.pageData.avatar == '' ? '' : this.pageData.avatar
				this.nickname = this.pageData.nickname == null || this.pageData.nickname == '' ? '' : this.pageData
					.nickname
				this.phone = this.pageData.username == null || this.pageData.username == '' ? '' : this.pageData
					.username
				this.sex = this.pageData.sex == null || this.pageData.sex == '' ? '' : this.pageData.sex
				this.date1 = this.pageData.birthday == null || this.pageData.birthday == '' ? '' : this.pageData
					.birthday
				console.log("用户信息", res)
			},

			handleAvatar() {
				let that = this
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], //从相册选择
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						console.log("tempFilePaths", tempFilePaths)
						uni.uploadFile({
							url: 'http://admin.jinsekeji.com.cn/api/login/upload', //仅为示例，非真实的接口地址
							filePath: tempFilePaths[0],
							name: 'file',
							header: {
								// "Content-Type": "application/form-data",
								"Authorization": 'Bearer' + ' ' + that.token
							},
							success: (uploadFileRes) => {
								// console.log("33333", uploadFileRes)
								that.avatar = (JSON.parse(uploadFileRes.data).data)
								console.log("图片", that.avatar)
							},
							fail(e) {
								console.log("上传图片", e)
							}
						});
					}
				});
			},
			onInputFocus() {
				this.show = true
				uni.hideKeyboard(); // 隐藏键盘
			},

			handleconfirmDate(e) {
				this.show = false
				this.date = this.timestampToTime(e.value)
				this.date1 = this.date
				console.log("时间", this.date)
			},

			//时间戳转化日期
			timestampToTime(timestamp) {
				// 时间戳为10位需*1000，时间戳为13位不需乘1000
				var date = new Date(timestamp);
				var Y = date.getFullYear() + "-";
				var M =
					(date.getMonth() + 1 < 10 ?
						"0" + (date.getMonth() + 1) :
						date.getMonth() + 1) + "-";
				var D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " ";
				var h = date.getHours() + ":";
				var m = date.getMinutes() + ":";
				var s = date.getSeconds();
				return Y + M + D;
			},
			handleconfirmSex(e) {
				console.log("性别", e)
				this.sex = e.value[0]
				this.sexshow = false
			},

			async handleSave() {
				let data = {
					avatar: this.avatar,
					nickname: this.nickname,
					sex: this.sex,
					birthday: this.date1,
				}
				let res = await checkuser(data)
				if (res.code == 1000) {
					uni.showToast({
						title: "编辑成功",
						icon: "none",
						duration: 1500
					});
					setTimeout(() => {
						uni.navigateBack({
							delta: 1
						})
					}, 2000)
				} else {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 1500
					});
				}
			},

		}
	}
</script>

<style scoped lang="scss">
	.userInfoBox {


		.infoitem {
			width: 100%;
			height: 104rpx;
			background: #FFFFFF;
			padding: 30rpx 40rpx;
			box-sizing: border-box;
			display: flex;
			justify-content: space-between;
			margin-top: 2rpx;

			.left {
				font-size: 28rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 400;
				color: #333333;
				line-height: 40rpx;
			}

			.right {
				display: flex;
				align-items: center;
				font-size: 28rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 400;
				line-height: 40rpx;
			}
		}

	}

	.btns {
		width: 542rpx;
		height: 80rpx;
		background: #04767C;
		border-radius: 512rpx 512rpx 512rpx 512rpx;
		font-size: 36rpx;
		font-family: PingFang SC-Bold, PingFang SC;
		font-weight: 400;
		color: #FFFFFF;
		line-height: 80rpx;
		text-align: center;
		margin: 0 auto;
		margin-top: 196rpx;
	}
</style>